
import 'dart:io';

import 'package:interrogation/src/data/models/consultation_res.dart';
import 'package:interrogation/src/data/models/get_consultation_res.dart';

import '../base/network/index.dart';
import 'models/char_record_res.dart';
import 'models/doctor_list_res.dart';
import 'models/get_article_res.dart';
import 'models/get_info_res.dart';
import 'models/login_res.dart';

class Api {
  static String gateway = "http://192.168.40.198:48080";
  static  final Http _http = Http();

 static Future<LoginRes?> login({
    required String username,
    required String password,
  }) async {
    try {
      final response = await _http.post(
        '/admin-api/system/auth/login',
        data: {
          'username': username,
          'password': password,
        },
      );
      if (response != null) return LoginRes.fromJson(response);
      return null;
    } catch (e) {
      throw Exception('Login failed: $e');
    }
  }

  static Future<UserInfo?> getUserInfo() async {
    try {
      final response = await _http.get(
        '/admin-api/system/user/profile/get',
      );
      if (response != null) return GetInfoRes.fromJson(response).data;
      return null;
    } catch (e) {
      throw Exception('user  profile  profile failed: $e');
    }
  }

  static Future<List<DoctorModel>?> getDoctorList({
    int? page,
    int? pageSize,
    String? keywords,
  }) async {
    try {
      final response = await _http.get(
        '/admin-api/modules/doctor/page',
        params: {
          'pageNo': page,
          'pageSize': pageSize,
        },
      );
      if (response != null)
        return DoctorListRes.fromJson(response).data?.list ?? [];
      return null;
    } catch (e) {
      throw Exception('DoctorListRes failed: $e');
    }
  }

  static Future loginOut() async {
    try {
      final response = await _http.post(
        '/admin-api/system/auth/logout',
        data: {},
      );
      if (response != null) return HttpStatus.ok;
      return null;
    } catch (e) {
      throw Exception('Login failed: $e');
    }
  }

  static Future<ConsultationRes?> postConsultation(Consultation req) async {
    try {
      final response = await _http.post(
        '/admin-api/chat/consultation/create',
        data: req.toJson(),
      );
      if (response != null) return  ConsultationRes.fromJson(response);
      return null;
    } catch (e) {
      throw Exception('Login failed: $e');
    }
  }

  static Future<CharRecordRes?> getChatMessageList(
    int cid, {
    int? page,
    int? pageSize,
  }) async {
    try {
      final response = await _http.get(
        '/admin-api/chat/consultation-message/chat-records',
        params: {
          "cid": cid,
          'pageNo': page,
          'pageSize': pageSize,
        },
      );
      if (response != null)
        return CharRecordRes.fromJson(response);
      return null;
    } catch (e) {
      throw Exception('DoctorListRes failed: $e');
    }
  }
  static Future<List<ConsultationModel>?> getConsultationList({
    int? page,
    int? pageSize,
    String? keywords,
  }) async {
    try {
      final response = await _http.get(
        '/admin-api/chat/consultation/history',
        params: {
          'pageNo': page,
          'pageSize': pageSize,
        },
      );
      if (response != null)
        return GetConsultationRes.fromJson(response).data?.list ?? [];
      return null;
    } catch (e) {
      throw Exception('getConsultationListRes failed: $e');
    }
  }
  static Future<List<ArticleModel>?> getArticleList({
    int? page,
    int? pageSize,
    String? keywords,
  }) async {
    try {
      final response = await _http.get(
        '/admin-api/modules/health-science/page',
        params: {
          'pageNo': page,
          'pageSize': pageSize,
        },
      );
      if (response != null)
        return GetArticleRes.fromJson(response).data?.list ?? [];
      return null;
    } catch (e) {
      throw Exception('getConsultationListRes failed: $e');
    }
  }



}

